﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Framework;
using TjZheng.Web.Mvc;
using TjZheng.WeiXin.SDK;
using TjZheng.WeiXin.SDK.Pay;

namespace TjZheng.WeiXin.Web.Controllers
{
    public class NativePayServiceController : TjZhengController
    {
        /// <summary>
        /// 扫码支付模式一
        /// </summary>
        /// <param name="merchantId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ActionResult ModeOne(int? merchantId, string productId)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!merchantId.HasValue)
            {
                objInvokeResult.ResultCode = "MerchantIdNull";
                objInvokeResult.ResultMessage = "MerchantId为空";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            if (productId.IsNull())
            {
                objInvokeResult.ResultCode = "ProductIdNull";
                objInvokeResult.ResultMessage = "ProductId为空";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }

            NativePay objNativePay = new PayModeOne(merchantId.Value);
            WxPayPackage objWxPayPackage = new WxPayPackage();
            objWxPayPackage.SetValue("product_id", productId);//商品ID

            objInvokeResult = objNativePay.CreatePayUrl(objWxPayPackage);
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 扫码支付模式一回调地址，配置路径：微信公众号平台-微信支付-扫码支付
        /// </summary>
        /// <returns></returns>
        public ActionResult ModeOneCallBack()
        {
            ModeOneServiceProcessContext objModeOneServiceProcessContext = new ModeOneServiceProcessContext();

            //接受微信支付系统回传的数据包
            WxPayPackage objWxPayPackage = WxPayPackage.ParseFromStream(Request.InputStream);
            objModeOneServiceProcessContext.SetIntputWxPayPackage(objWxPayPackage);
            ModeOneServiceProcessHandle objModeOneServiceProcessHandle = new ModeOneServiceProcessHandle();
            InvokeResult objInvokeResult = objModeOneServiceProcessHandle.ExecuteProcess(objModeOneServiceProcessContext);
            WxPayPackage objOutlPutWxPayPackage = null;
            if (objInvokeResult.ResultCode != "Success")
            {
                objOutlPutWxPayPackage = new WxPayPackage();
                objOutlPutWxPayPackage.SetValue("return_code", "FAIL");
                objOutlPutWxPayPackage.SetValue("return_msg", "统一下单失败");
                return Content(objOutlPutWxPayPackage.ConvertToXml());
            }

            objOutlPutWxPayPackage = objInvokeResult.Data as WxPayPackage;
            return Content(objOutlPutWxPayPackage.ConvertToXml());
        }

        /// <summary>
        /// 扫码支付模式二
        /// </summary>
        /// <returns></returns>
        public ActionResult ModeTwo()
        {
            ModeTwoServiceProcessContext objModeTwoServiceProcessContext = new ModeTwoServiceProcessContext();
            objModeTwoServiceProcessContext.SetWeiXinPackage(WeiXinPackage.ParseFromStream(this.Request.InputStream));

            ModeTwoServiceProcessHandle objModeTwoServiceProcessHandle = new ModeTwoServiceProcessHandle();
            return Json(objModeTwoServiceProcessHandle.ExecuteProcess(objModeTwoServiceProcessContext), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 支付成功微信支付系统异步消息通支付结果
        /// </summary>
        /// <returns></returns>
        public ActionResult PaySuccessNotify()
        {
            NotifyProcessContext objNotifyProcessContext = new NotifyProcessContext();
            WxPayPackage objWxPayPackage = WxPayPackage.ParseFromStream(Request.InputStream);
            objNotifyProcessContext.SetIntputWxPayPackage(objWxPayPackage);

            NotifyProcessHandle objNotifyProcessHandle = new NotifyProcessHandle();
            InvokeResult objInvokeResult = objNotifyProcessHandle.ExecuteProcess(objNotifyProcessContext);
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }
    }
}
